package com.pls.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.pls.actionObjects.GetEmployeeDetailsRequest;
import com.pls.actionObjects.GetEmployeeDetailsResponse;
import com.pls.actionObjects.GetWorkstationMapByNameRequest;
import com.pls.actionObjects.GetWorkstationMapByNameResponse;
import com.pls.constants.ValidationErrorEnum;
import com.pls.dao.DAOFactory;
import com.pls.dao.EmployeeDAO;
import com.pls.dao.ProjectDAO;
import com.pls.domain.EmployeeProjectMap;
import com.pls.domain.EmployeeWorkstation;
import com.pls.domain.ExceptionError;
import com.pls.domain.Project;
import com.pls.domain.ValidationError;

public class GetWorkstationMapByNameHandler
		extends
		BaseRequestHandler<GetWorkstationMapByNameResponse, GetWorkstationMapByNameRequest> {

	public GetWorkstationMapByNameHandler(
			Class<GetWorkstationMapByNameResponse> tResponse) {
		super(tResponse);
		// TODO Auto-generated constructor stub
	}

	protected ArrayList<ValidationError> validate(
			GetWorkstationMapByNameRequest request) {
		logger.debug("- validate method entered");
		ArrayList<ValidationError> errors = new ArrayList<ValidationError>();
		ValidationError valError;

		// Check if request is null
		if (request == null) {
			valError = new ValidationError();
			valError.setMessage("Request is null.");
			valError.setErrorName(ValidationErrorEnum.NullRequest);
			errors.add(valError);
			logger.debug(this.getClass()
					+ "- null request. Exiting validate method.");
			return errors;
		}

		// check if employee first name is null
		if (request.getFirstName() == null) {
			valError = new ValidationError();
			valError.setMessage("First name cannot be null.");
			valError.setErrorName(ValidationErrorEnum.InvalidRequestParameter);
			errors.add(valError);
		}

		// check if employee first name is null
		if (request.getLastName() == null) {
			valError = new ValidationError();
			valError.setMessage("Last name cannot be null.");
			valError.setErrorName(ValidationErrorEnum.InvalidRequestParameter);
			errors.add(valError);
		}

		logger.debug("- validate method exited.");
		return errors;
	}

	protected GetWorkstationMapByNameResponse process(
			GetWorkstationMapByNameRequest request) {

		GetWorkstationMapByNameResponse response = new GetWorkstationMapByNameResponse();
		// call data layer
		EmployeeDAO empDao = DAOFactory.getEmployeeDAO();
		ProjectDAO projDao = DAOFactory.getProjectDAO();
		List<EmployeeWorkstation> empWorkstationList = new ArrayList<EmployeeWorkstation>();
		EmployeeProjectMap employeeProjMap = new EmployeeProjectMap();

		try {
			logger.debug("- process method entered.");

			// Get the list of workstations of the quadrant of the employee
			empWorkstationList = empDao
					.getEmployeeWorkStationMapByFirstNameAndLastName(
							request.getFirstName(), request.getLastName());

			// Get the all projects
			employeeProjMap = projDao.getEmployeeProjects();

			// Map the projects to the employees
			for (EmployeeWorkstation empWorkstation : empWorkstationList) {

				int empId = empWorkstation.getEmployee().getEmployeeId();

				if (employeeProjMap.getEmployeeProjectMap().containsKey(empId)) {
					empWorkstation.setProjectList(employeeProjMap
							.getEmployeeProjectMap().get(empId));
				}

			}

			// set the response
			response.setWorkstationList(empWorkstationList);

		} catch (SQLException e) {
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an SQL error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
			logger.debug(this.getClass() + "- sql exception:" + e.getMessage());
		} catch (Exception e) {
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an error occurred.");
			response.setExceptionError(sqlError);
			logger.debug(this.getClass() + "- exception:" + e.getMessage());
		}

		logger.debug(this.getClass() + "- process method exited.");
		return response;
	}

}
